import EventEmitter from './class/EventEmitter'
import { io } from 'socket.io-client'
export class FivePoint extends EventEmitter {
  constructor(context) {
    super()
    this.context = context
    this.row = 20
    this.col = 20
    this.list = []
    this.history = []
    this.nowTurn = 0 //0蓝色 1红色
    this.isOver = false
    this.msg = ''
    this.wsUrl = 'ws://192.168.110.108:3000'
    this.users = [
      {
        name: '白棋',
        value: 0
      },
      {
        name: '黑棋',
        value: 1
      }
    ]
    this.userId = -1
  }
  reset() {
    this.nowTurn = 0
    this.isOver = false
    this.list.forEach((res) => {
      res.selected = false
      res.user = 0
    })
    this.history = []
    this.msg = ''
  }
  initSocket() {
    const socket = io(this.wsUrl)
    socket.on('chat', (from) => {
      const { x, y } = from
      let item = this.getItem([x, y])
      item.selected = true
      item.user = this.nowTurn + 1
      this.history.push(item)
      if (!this.checkSuccess(item)) {
        this.nowTurn = 1 - this.nowTurn
      }
    })
    socket.on('back', () => {
      let item = this.history.pop()
      item.selected = false
      item.user = 0
      this.nowTurn = 1 - this.nowTurn
    })
    this.socket = socket
  }
  init() {
    const { row, col } = this
    for (let i = 0; i < row; i++) {
      for (let j = 0; j < col; j++) {
        let item = {
          x: j,
          y: i,
          selected: false,
          user: 0
        }
        this.list.push(item)
      }
    }
    this.initSocket()
  }
  revocation() {
    if (this.isOver) return
    if (this.nowTurn != this.userId) {
      this.socket.emit('back')
    }
  }
  testSelect() {}
  selectItem(item) {
    if (this.userId == -1) {
      this.context.$Message.info('请选择选手')
      return
    }
    if (!item.selected && !this.isOver && this.userId == this.nowTurn) {
      this.socket.emit('server', item)
    }
  }
  //选中
  select(x, y, user) {
    let item = this.list.find((res) => {
      return res.x == x && res.y == y
    })
    item.selected = true
    item.user = user
  }
  checkSuccess(item) {
    const { x, y } = item
    if (this.checkPoint([x, y])) {
      console.log(`win`)
      let msg = `${this.nowTurn == 0 ? '白方' : '黑方'}获胜`
      this.msg = msg
      this.isOver = true
      return true
    } else {
      return false
    }
  }
  checkPoint(point) {
    //新思路
    //以选中点的开始范围，第一个点~第五个点，然后找出关联的点位
    const [x, y] = point
    //这一行直接取出来
    let rowList = []
    let colList = []
    let backslashList = []
    let slashList = []
    for (var i = 0; i < this.col; i++) {
      rowList.push(this.getItem([x, i]))
    }
    for (var i = 0; i < this.row; i++) {
      colList.push(this.getItem([i, y]))
    }

    //让x变成0，然后y从坐标开始递增
    let x1 = 0
    let y1 = y - x
    for (var i = 0; i < this.row; i++) {
      backslashList.push(this.getItem([i, y1 + i]))
    }

    // 3,5
    let x2 = 0
    let y2 = x + y
    for (var i = 0; i < this.row; i++) {
      slashList.push(this.getItem([i, y2 - i]))
    }

    let flag
    for (var i = 0; i < 5; i++) {
      //起点
      flag =
        this.checkStartPoint(rowList, i, x, y) || this.checkStartPoint(colList, i, x, y) || this.checkStartPoint(backslashList, i, x, y) || this.checkStartPoint(slashList, i, x, y)
      if (flag) {
        console.log(`flag`, flag)
        break
      }
    }
    return flag
  }
  checkStartPoint(l, i, x, y) {
    let index = l.findIndex((res) => res && res.x == x && res.y == y)
    let s = index - i
    let e = s + 5
    let list = l.slice(s, e)
    let flag = false
    if (list.length == 5) {
      flag = this.checkSame(list)
    }
    if (flag) {
      console.log(list, 'list')
    }
    return flag
  }
  checkSame(list) {
    if (list[0] && list[0].user) {
      return list.every((res) => res && res.user == list[0].user)
    } else {
      return false
    }
  }
  getItem(point) {
    const [x, y] = point
    return this.list.find((res) => res.x == x && res.y == y)
  }
}
